/* ActorService.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package services;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import repositories.BidRepository;
import domain.Bid;
import domain.Customer;

@Service
@Transactional
public class BidService {

	// Managed repository -----------------------------------------------------

	@Autowired
	private BidRepository bidRepository;
	

	// Supporting services ----------------------------------------------------
	@Autowired
	private AuctionService auctionService;


	private ActorService actorService;
	
	// Constructors -----------------------------------------------------------

	public BidService() {
		super();
	}

	// Simple CRUD methods ----------------------------------------------------

	public void modify(Bid bid) {
		bidRepository.save(bid);
	}
	// Other business methods -------------------------------------------------

	
	public Bid findOne(int bidId){
		return bidRepository.findOne(bidId);
	}
	
	public Collection<Bid> findBidsByAuctionId(int auctionId){
		return bidRepository.findBidsByAuctionId(auctionId);
	}
	
	public Collection<Bid> findHighestBid(){
		return bidRepository.findHighestBid();

	}

	public Bid create(int auctionId) {
		Bid bid = new Bid();
		bid.setMoment(new Date());
		bid.setAuction(auctionService.findOne(auctionId));
		Assert.isTrue(actorService.findByPrincipal() instanceof Customer);
		bid.setCustomer((Customer) actorService.findByPrincipal());
		return null;
	}

	public void save(Bid bid) {
		Assert.isTrue(actorService.findByPrincipal() instanceof Customer);
		bidRepository.save(bid);
		
		
	}
}